package com.baidumap.administrator.happystudy.ui;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.ab.dialog.loadingDialog.LoadingDialogUtil;
import com.ab.http.OnMessageResponse;
import com.ab.util.AbStrUtil;
import com.ab.util.ToastUtil;
import com.ab.view.clear_edit_text.ClearEditText;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.model.SendSMS;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 描述：修改密码
 * 作者：xiaoxing on 17/4/7 15:07
 * 邮箱：2235445233@qq.com
 */
public class ModifyPwdActivity extends AppBaseActivity implements OnMessageResponse {

    @Bind(R.id.cet_tel)
    ClearEditText cetTel;

    private String mTel;

    @Override
    public int bindLayout() {
        return R.layout.activity_modify_pwd;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
    }


    @OnClick(R.id.btn_next_step)
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.btn_next_step:
                next();
                break;

        }
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        setHeaderTitle(R.string.modify_pwd);
    }

    /**
     * 下一步
     */
    private void next() {

        if (checkTel()) {
            LoadingDialogUtil.showGifdialog(getSupportFragmentManager());
            Api.sendCheckCode(this, mTel);


        }
    }

    /**
     * 验证手机号
     *
     * @return
     */
    private boolean checkTel() {
        mTel = getEidtTextValue(cetTel);

        if (AbStrUtil.isEmpty(mTel)) {
            ToastUtil.showMessage(this, "手机号不能为空");
            return false;
        } else if (!AbStrUtil.isMobileNo(mTel)) {
            ToastUtil.showMessage(this, "手机号格式错误");
            return false;
        }
        return true;
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.SENE_CHECK_CODE) && jo != null) {
            SendSMS sendSMS = new SendSMS(jo.toString());

            if (sendSMS != null) {


                if (sendSMS.getCode().equals("200")) {

                    ToastUtil.showMessage(this, "验证码发送成功");
                    Bundle bundle = new Bundle();
                    bundle.putString("tel", mTel);

                    startBundleActivity(bundle, ModifyPwdActivity2.class);
                } else {

                    ToastUtil.showMessage(this, sendSMS.getMsg());

                }
            }

        }

    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
